package com.jzcodecheck.ui.core.reference;

import java.awt.event.KeyEvent;

import javax.swing.Action;

import com.jzcodecheck.ui.core.BatchUI;
import com.jzcodecheck.ui.core.action.AboutDialogAction;
import com.jzcodecheck.ui.core.action.AbstractUIAction;
import com.jzcodecheck.ui.core.action.CodeNoteScanAction;
import com.jzcodecheck.ui.core.action.SaveProjectAction;
import com.jzcodecheck.ui.core.action.SystemExitAction;
import com.jzcodecheck.ui.core.registry.MenuRegistry;
import com.jzcodecheck.ui.core.registry.NavigationRegistry;
import com.jzcodecheck.ui.core.registry.ToolBarRegistry;
import com.jzcodecheck.ui.helper.ImageHelper;
import com.jzcodecheck.ui.helper.UIPropertyHelper;

public class UIReference
  implements Reference
{
  private MenuRegistry menuRegistry;
  private NavigationRegistry navigationRegistry;
  private ToolBarRegistry barRegistry;
  private BatchUI ui;
  private static final ImageHelper imager = new ImageHelper();
  public UIReference(BatchUI ui)
  {
    this.ui = ui;
    this.menuRegistry = MenuRegistry.getInstance();
    this.navigationRegistry = NavigationRegistry.getInstance();
    this.barRegistry = ToolBarRegistry.getInstance();
  } 

  public void init() {
    menuRegistry();
    barRegistry();
  }

  public void destroy() {
    this.menuRegistry.clear();
    this.barRegistry.clear();
    this.navigationRegistry.clear();
  }

  private void barRegistry()
  {
	  Action action = new CodeNoteScanAction(ui);
	  int ctrlKey = KeyEvent.CTRL_MASK;
	  //扫描代码目录
	  barRegistry.registry("CodeNoteScan", imager.NEWPROJECT_ICON, UIPropertyHelper.getString("menu.file.codenotescan"), action,
			  UIPropertyHelper.getMnemonic("menu.file.codenotescan.mnemonic"),ctrlKey);
	  //保存结果
	  barRegistry.registry("SaveResult", imager.SAVEPROJECT_ICON, UIPropertyHelper.getString("menu.file.resultsave"), new SaveProjectAction(ui),
			  UIPropertyHelper.getMnemonic("menu.file.resultsave.mnemonic"),ctrlKey);
	  //关于
	  barRegistry.registry("About", imager.AUTOSCRIPT_ICON, UIPropertyHelper.getString("menu.about"), new AboutDialogAction(ui),
			  UIPropertyHelper.getMnemonic("menu.about.mnemonic"),ctrlKey);
	  //退出
	  barRegistry.registry("Exit", imager.EXIT_ICON, UIPropertyHelper.getString("menu.file.exit"), new SystemExitAction(ui),
			  UIPropertyHelper.getMnemonic("menu.file.exit.mnemonic"),ctrlKey);	  
  }

  private void menuRegistry() {
	
    this.menuRegistry.registry("fileMenu", getString("menu.file"));
	this.menuRegistry.registryItem("fileMenu", getString("menu.file.codenotescan"), getMnemonic("menu.file.codenotescan.mnemonic"), getString("menu.file.codenotescan.description"), false, new CodeNoteScanAction(ui));
    this.menuRegistry.registryItem("fileMenu", getString("menu.file.resultsave"), getMnemonic("menu.file.resultsave.mnemonic"), getString("menu.file.resultsave.description"), false, new SaveProjectAction(ui));

    this.menuRegistry.registryItem("fileMenu", getString("menu.file.exit"), getMnemonic("menu.file.mnemonic"), getString("menu.file.description"), true, new SystemExitAction(this.ui));
    
    this.menuRegistry.registry("help", getString("menu.help"));
    this.menuRegistry.registryItem("help", getString("menu.about"), getMnemonic("menu.about.mnemonic"), getString("menu.about.description"), false, new AboutDialogAction(this.ui));
  }

 

  private char getMnemonic(String key) {
    return getString(key).charAt(0);
  }

  private String getString(String key) {
    return UIPropertyHelper.getString(key);
  }
}

